﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Microsoft.JCI.Common.Entity;
using Microsoft.JCI.Bll;
using System.Collections.Generic;
using Microsoft.JCI.Common.Utility;
using System.Text;

namespace Microsoft.JCI.Portal.Dispatch
{
    public partial class WorkItemAdd2 : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {               
                if (Request.QueryString["DispatchFormID"] != null)
                {
                    DispatchFormID = new Guid(Request.QueryString["DispatchFormID"]);
                    LoadData();
                }

                if (Request.QueryString["WorkOrderID"] != null)
                {
                    WorkingVendorOrderID = new Guid(Request.QueryString["WorkOrderID"]);                 
                }
                BindGrid();
            }
        }    

        private void LoadData()
        {
            if (DispatchFormID == Guid.Empty) return;

            DispatchBll bll = new DispatchBll();
            DisDispatchFormEntity entity = bll.Get<DisDispatchFormEntity>(DispatchFormID);
            if (entity != null)
            {
                if (entity.CustomerId != Guid.Empty)
                {
                    ucCustomerCtrlInfo.CustomerID = entity.CustomerId;
                    ucCustomerCtrlInfo.Enabled = false;

                    CustomerID = entity.CustomerId;
                }
                
            }
        }

        public Guid DispatchFormID
        {
            get
            {
                if (string.IsNullOrEmpty(txtDispatchFormID.Value)) return Guid.Empty;
                else return new Guid(txtDispatchFormID.Value);
            }
            set
            {
                txtDispatchFormID.Value = value.ToString();
            }
        }

        public Guid WorkingVendorOrderID
        {
            get
            {
                if (string.IsNullOrEmpty(txtWorkingVendorOrderId.Value)) return Guid.Empty;
                else return new Guid(txtWorkingVendorOrderId.Value);
            }
            set
            {
                txtWorkingVendorOrderId.Value = value.ToString();
            }
        }

        public Guid CustomerID
        {
            get
            {
                if (string.IsNullOrEmpty(txtCustomerID.Value)) return Guid.Empty;
                else return new Guid(txtCustomerID.Value);
            }
            set
            {
                txtCustomerID.Value = value.ToString();
            }
        }

        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            ucCustomerCtrlInfo.Enabled = true;
            ucCustomerCtrlInfo.GetCustomerInfo();
            CustomerID = Guid.NewGuid();
        } 

        private void BindGrid()
        {
            DisWorkingVendorFileItemBll bll = new DisWorkingVendorFileItemBll();
            if (WorkingVendorOrderID == Guid.Empty) return;

            DisWorkingVendorFileItemEntity entity = bll.Get<DisWorkingVendorFileItemEntity>(" and WorkingVendorOrderId = '" + WorkingVendorOrderID.ToString() + "'");

            this.txtCustomerName.Text = entity.CustomerName;
            this.txtDebugDate.Text = entity.DebugDate.ToString();
            this.txtInMNO.Text = entity.InMNO;
            this.txtInMType.Text = entity.InMType;
            this.txtOutMNO.Text = entity.OutMNO;
            this.txtOutMType.Text = entity.OutMType;
            this.txtPhone.Text = entity.Phone;
            this.ddlCustomerType.SelectedValue = entity.CustomerType;
            this.txtSalesUnit.Text = entity.SalesUnit;
            this.txtTicketDate.Text = entity.TicketDate;
            this.txtTicketNo.Text = entity.TicketNo;
            this.txtVenderName.Text = entity.VenderName;
            this.txtUnitName.Text = entity.UnitName;
            this.txtAddress.Text = entity.Address;
            this.txtBPNo.Text = entity.BPNo;
            

            List<DisWorkingVendorFileItemEntity> list = new DisWorkingVendorFileItemBll().GetAll<DisWorkingVendorFileItemEntity>(" and WorkingVendorOrderId = '" + WorkingVendorOrderID.ToString() + "'");
            RepeaterDocuments.DataSource = list;
            RepeaterDocuments.DataBind();
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            UploadFileBaseInfo uploadInfo = this.fuFilePath.UploadFile(Server.MapPath("../Uploads"));  

            if (uploadInfo.IsSuccess)
            {
                DisWorkingVendorOrderEntity OrderEntity = new DisWorkingVendorOrderEntity();
                if (WorkingVendorOrderID != Guid.Empty)
                {
                    OrderEntity.WorkingVendorOrderId = WorkingVendorOrderID;
                }
                else
                {
                    OrderEntity.WorkingVendorOrderId = Guid.NewGuid();
                    WorkingVendorOrderID = OrderEntity.WorkingVendorOrderId;
                }            
                OrderEntity.DispatchFormId = DispatchFormID;
                OrderEntity.CustomerId = CustomerID;
                OrderEntity.ProductId = Guid.Empty;
                OrderEntity.WorkingType = (int)Enumerator.WorkType.BootDebugging;
                OrderEntity.CreateOn = DateTime.Now;
                OrderEntity.CreateBy = base.CurrentUserEntity.Account;
                OrderEntity.IsChecked = false;

                DisWorkingVendorFileItemEntity entity = new DisWorkingVendorFileItemEntity();
                entity.WorkingVendorOrderId = WorkingVendorOrderID;
                entity.ID = Guid.NewGuid();
                entity.FilePath = uploadInfo.FilePath;
                entity.CreateTime = System.DateTime.Now;
                entity.Deleted = false;
                entity.FileName = uploadInfo.FileName;
                entity.FileOriginalName = uploadInfo.FileOriginalName;
                DisWorkingVendorFileItemBll bll = new DisWorkingVendorFileItemBll();
                bll.SaveNewWorkingOrder(OrderEntity,entity);
                ClientScript.RegisterStartupScript(this.GetType(), "success", "alert('上传成功');", true);
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "error", "alert('" + uploadInfo.Message + "')", true);
            }
            BindGrid();
        }

        protected void RepeaterDocuments_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "download")
            {
                WebUtility.DownloadFile(e.CommandArgument.ToString().TrimStart('\\'), "", "");
            }
            if (e.CommandName.ToLower() == "delete") {
                DisWorkingVendorFileItemBll bll = new DisWorkingVendorFileItemBll();
                bll.Delete<DisWorkingVendorFileItemEntity>(" and ID = '" + e.CommandArgument.ToString() + "'");
                BindGrid();
            }
        }

        protected void lnkbtnSaveAndClose_Click(object sender, EventArgs e)
        {
            UploadFileBaseInfo uploadInfo = this.fuFilePath.UploadFile(Server.MapPath("../Uploads"));

            if (uploadInfo.IsSuccess)
            {
                DispatchBll dbll = new DispatchBll();

                MadCustomerEntity CustomerEntity = ucCustomerCtrlInfo.CustomerEntity;
                CustomerEntity.CustomerId = CustomerID;
                dbll.InsertOrUpdate(CustomerEntity);



                DisWorkingVendorOrderEntity OrderEntity = new DisWorkingVendorOrderEntity();
                if (WorkingVendorOrderID != Guid.Empty)
                {
                    OrderEntity.WorkingVendorOrderId = WorkingVendorOrderID;
                }
                else
                {
                    OrderEntity.WorkingVendorOrderId = Guid.NewGuid();
                    WorkingVendorOrderID = OrderEntity.WorkingVendorOrderId;
                }
                OrderEntity.DispatchFormId = DispatchFormID;
                OrderEntity.CustomerId = CustomerID;
                OrderEntity.ProductId = Guid.Empty;
                OrderEntity.WorkingType = (int)Enumerator.WorkType.BootDebugging;
                OrderEntity.CreateOn = DateTime.Now;
                OrderEntity.CreateBy = base.CurrentUserEntity.Account;
                OrderEntity.IsChecked = false;

                DisWorkingVendorFileItemEntity entity = new DisWorkingVendorFileItemEntity();
                entity.WorkingVendorOrderId = WorkingVendorOrderID;
                entity.ID = Guid.NewGuid();
                entity.FilePath = uploadInfo.FilePath;
                entity.CreateTime = System.DateTime.Now;
                entity.Deleted = false;
                entity.FileName = uploadInfo.FileName;
                entity.FileOriginalName = uploadInfo.FileOriginalName;

                entity.CustomerName = txtCustomerName.Text.Trim();
                entity.CustomerType = ddlCustomerType.SelectedValue;
                entity.Phone = txtPhone.Text.Trim();
                entity.Address = txtAddress.Text.Trim();
                entity.SalesUnit = txtSalesUnit.Text.Trim();
                entity.TicketDate = txtTicketDate.Text.Trim();
                entity.TicketNo = txtTicketNo.Text.Trim();
                entity.UnitName = txtUnitName.Text.Trim();
                entity.VenderName = txtVenderName.Text.Trim();
                entity.InMNO = txtInMNO.Text.Trim();
                entity.OutMNO = txtOutMNO.Text.Trim();
                entity.InMType = txtInMType.Text.Trim();
                entity.OutMType = txtOutMType.Text.Trim();
                entity.DebugDate = txtDebugDate.Text.Trim();
                entity.BPNo = txtBPNo.Text.Trim();
               

                DisWorkingVendorFileItemBll bll = new DisWorkingVendorFileItemBll();
                bll.SaveNewWorkingOrder(OrderEntity, entity);

               
                ClientScript.RegisterStartupScript(this.GetType(), "success", "alert('保存成功!');window.close();", true);
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "error", "alert('" + uploadInfo.Message + "')", true);
            }
            BindGrid();
        } 

    }
}
